package de.lusiardi.webdav.xml;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

/**
 * http://www.webdav.org/specs/rfc4918.html#ELEMENT_timeout
 * http://www.webdav.org/specs/rfc4918.html#HEADER_Timeout
 *
 * @author Joachim Lusiardi
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "timeout")
public class TimeOut {

    public static final String INFINITE = "Infinite";
    public static final String SECOND = "Second-";

    private static final Logger logger = Logger.getLogger(TimeOut.class.getCanonicalName());

    @XmlValue
    private String timeType;

    public String getTimeType() {
        return timeType;
    }

    public void setTimeType(String timeType) {
        this.timeType = timeType;
        if (!checkFormat()) {
            logger.warning(String.format("'%s' is an invalid time type date.", timeType));
        }
    }

    public boolean checkFormat() {
        if (timeType == null) {
            return false;
        }
        Pattern datePattern = Pattern.compile("^(Second-\\d+|Infinite)$");
        Matcher dateMatcher = datePattern.matcher(timeType);

        if (!dateMatcher.matches()) {
            return false;
        }
        return true;
    }
}
